import { contextBridge, ipcRenderer } from 'electron';

const curdList = [
  'user',
  'role',
  'paramConfig',
  'dictionaryType',
  'dictionary',
  'machine',
  'material',
  'order',
  'workType',
  'shift',
  'custom',
  'orderReport',
];

function setCurdObjFn() {
  const obj = {};

  for (const item of curdList) {
    obj[`${item}Page`] = (...arg) => ipcRenderer.invoke(`${item}/page`, ...arg);
    obj[`${item}Add`] = (...arg) => ipcRenderer.invoke(`${item}/add`, ...arg);
    obj[`${item}Update`] = (...arg) => ipcRenderer.invoke(`${item}/update`, ...arg);
    obj[`${item}Delete`] = (...arg) => ipcRenderer.invoke(`${item}/delete`, ...arg);
  }

  return obj;
}

// console.log(setCurdObjFn())

contextBridge.exposeInMainWorld('electron', {
  sendMsg: (msg: string): Promise<string> => ipcRenderer.invoke('msg', msg),
  onReplyMsg: (cb: (msg: string) => any) => {
    ipcRenderer.removeAllListeners('reply-msg');

    ipcRenderer.on('reply-msg', (_e, msg: any) => {
      cb(msg);
    });
  },
  onUpdateData: (cb: (data: any) => any) => {
    ipcRenderer.removeAllListeners('update-data');

    ipcRenderer.on('update-data', (_e, data: any) => {
      cb(data);
    });
  },
  onMessageData: (cb: (data: any) => any) => {
    ipcRenderer.removeAllListeners('message-data');

    ipcRenderer.on('message-data', (_e, data: any) => {
      cb(data);
    });
  },

  onHostOnline: (cb: (data: any) => any) => {
    ipcRenderer.removeAllListeners('host-online');

    ipcRenderer.on('host-online', (_e, data: any) => {
      cb(data);
    });
  },

  onHostOffline: (cb: (data: any) => any) => {
    ipcRenderer.removeAllListeners('host-offline');

    ipcRenderer.on('host-offline', (_e, data: any) => {
      cb(data);
    });
  },

  reload: () => ipcRenderer.invoke('reload'),

  login: (data) => ipcRenderer.invoke('login', data),
  initMenu: (list) => ipcRenderer.invoke('menu/initMenu', list),
  userInfo: (data) => ipcRenderer.invoke('user/info', data),
  userUpdatePassword: (data) => ipcRenderer.invoke('user/updatePassword', data),

  roleInfo: (data) => ipcRenderer.invoke('role/info', data),
  roleList: (data) => ipcRenderer.invoke('role/list', data),

  dictionaryTypeInfo: (data) => ipcRenderer.invoke('dictionaryType/info', data),
  dictionaryInfo: (data) => ipcRenderer.invoke('dictionary/info', data),

  fileUpload: (data) => ipcRenderer.invoke('file/upload', data),

  machineList: (data) => ipcRenderer.invoke('machine/list', data),
  machineByMac: (data) => ipcRenderer.invoke('machine/byMac', data),
  materialList: (data) => ipcRenderer.invoke('material/list', data),
  orderList: (data) => ipcRenderer.invoke('order/list', data),
  orderByMachineCode: (data) => ipcRenderer.invoke('order/byMachineCode', data),

  getMac: () => ipcRenderer.invoke('getMac'),
  getPlcData: () => ipcRenderer.send('getPlcData'),

  plcClick: (list) => ipcRenderer.send('plcClick', list),
  plcReConnect: (list) => ipcRenderer.send('plcReConnect', list),

  shiftInfo: (data) => ipcRenderer.invoke('shift/info', data),
  shiftList: (data) => ipcRenderer.invoke('shift/list', data),
  workTypeList: (data) => ipcRenderer.invoke('workType/list', data),
  customList: (data) => ipcRenderer.invoke('custom/list', data),
  userList: (data) => ipcRenderer.invoke('user/list', data),
  paramConfigInfo: (data) => ipcRenderer.invoke('paramConfig/info', data),
  orderUpdateAdjuest: (data, userInfo) => ipcRenderer.invoke('order/updateAdjuest', data, userInfo),
  orderReportConfirm: (data, userInfo) => ipcRenderer.invoke('orderReport/confirm', data, userInfo),
  pageColumnUpdate: (data, userInfo) => ipcRenderer.invoke('pageColumn/update', data, userInfo),
  pageColumnList: (data, userInfo) => ipcRenderer.invoke('pageColumn/list', data, userInfo),

  setConfig: (data) => ipcRenderer.invoke('setConfig', data),

  plcConnect: (data) => ipcRenderer.invoke('plcConnect', data),

  deleteChildProcess: () => ipcRenderer.invoke('deleteChildProcess'),

  ...setCurdObjFn(),
});
